new Vue({
	el: '#app',
	data: {
		uid: '',
		cid: '',
		coupon_id: '无',
		oid: '',
		study_id: '',
		isShow: false,
		study: '',
		cont: '',
		teacher_level: [],
		study_target_list: [],
		class_time: [],
		thumb_path: '',
		score_id: '',
		teacher_id: '',
		time_id: '',
		score_point: '',
		time_all: '',
		price_one: '',
		t_price: '',
		price: ''
	},
	mounted: function() {
		console.log("mounted end");
		this.init();
	},
	methods: {
		init: function() {
			var swiper = new Swiper('.swiper-container', {
				pagination: '.swiper-pagination',
				slidesPerView: 'auto',
				centeredSlides: true,
				paginationClickable: true,
				loop: true,
				autoplay: 5000
			});
			var self = this;
			self.uid = $api.getStorage('uid');
			self.api_token = $api.getStorage('api_token');
			//self.getMsg();
			apiready = function() {
				var fid = api.pageParam;
				self.cid = fid.id;
				self.coupon_id = fid.coupon_id;
				if (self.coupon_id) {
					api.closeWin({name:'myCoupon'});
				}
				//loading加载
				self.isS();
			}
		},
		getprice: function() {

			this.price = (parseFloat(this.t_price) + parseFloat(this.price_one)) * parseFloat(this.time_all);
			this.price = this.price.toFixed(2);
			if(this.price == 'NaN') {
				this.price = '';
			} else {

			}
		},
		isS: function() {
			var self = this;
			var UILoading = api.require('UILoading');
			if(self.isShow) {
				console.log(self.loadingId);
				UILoading.closeFlower({
					id: self.loadingId
				});
			} else {
				UILoading.flower({
					center: {
						x: api.winWidth / 2.0,
						y: api.winHeight / 2.0
					},
					size: 30,
					fixed: true
				}, function(ret) {
					console.log(JSON.stringify(ret));
					self.loadingId = ret.id;
				});
				console.log("get");
				self.getMsg();
			}
		},
		//获得课程信息
		getMsg: function() {
			var self = this;
			console.log(self.cid);
			var data = {
				id: self.cid
			};
			$.post(base + config.study_info, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.study = msg.study;
					self.price_one = msg.study.price;
					self.study_id = msg.study.id;
					self.teacher_level = msg.teacher_level;
					self.study_target_list = msg.study_target_list;
					self.thumb_path = msg.thumb_path;
					self.cont = msg.study.content;
					self.chooseScoreId(msg.study_target_list[0].id, msg.study_target_list[0].point);
					self.isShow = true;
					self.isS();
				} else {
					self.isShow = true;
					self.isS();
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//关闭本页面
		close: function() {
			api.closeWin();
		},
		//跳转到我的优惠券页面
		jumpDiscount: function() {
			api.openWin({
				name: 'myCoupon',
				url: '../user/myCoupon.html',
				reload: true,
				vScrollBarEnabled: false,
				pageParam: {
					cid: this.cid
				}
			});
		},
		//跳转到商品详情
		jumpProductDetails: function() {
			console.log(this.cont);
			api.openWin({
				name: 'productDetails',
				url: 'productDetails.html',
				vScrollBarEnabled: false,
				//				reload: true,
				pageParam: {
					cont: this.cont
				}
			});
		},
		//跳转到商品评价
		jumpProductEvaluation: function() {
			api.openWin({
				name: 'productEvaluation',
				url: 'productEvaluation.html',
				vScrollBarEnabled: false,
				//				reload: true,
				pageParam: {
					id: this.cid
				}
			});
		},
		//选择分数id
		chooseScoreId: function(id, point) {
			this.score_id = id;
			this.score_point = point;
			this.time(id);
			//			this.time_all = "1";
			this.getprice();
		},
		//老师称谓id
		chooseTeacherId: function(id, t_price) {
			this.teacher_id = id;
			this.t_price = t_price;
			this.getprice();
		},
		//获取课时列表
		time: function(id) {
			var self = this;
			var data = {
				id: id
			};
			$.post(base + config.study_lesson_count, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.class_time = msg.data;
				} else {
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//选择课时id
		chooseTimeId: function(id, time) {
			this.time_id = id;
			this.time_all = time;
			this.getprice();
		},
		//购买课程（下单）
		buy: function() {
			var self = this;
			if(self.uid) {
				var self = this;
				var data = {
					study_id: self.study_id,
					target_point: self.score_point,
					teacher_level: self.teacher_id,
					total: self.time_all,
					uid: self.uid
				};
				$.post(base + config.study_mall_order, data, function(msg) {
					console.log(msg);
					if(msg.error == 0) {
						self.oid = msg.study_order_id;
						console.log(self.oid);
						self.jumpBuy(self.oid);
						self.isShow = true;
						self.isS();
					} else {
						self.isShow = true;
						self.isS();
						console.log(msg.error_msg);
						api.toast({
							msg: msg.error_msg,
							duration: 2000,
							location: 'middle'
						});
					}
				}, 'json');
			} else {
				self.layout();
			}
		},
		jumpBuy: function(oid) {
			console.log(oid);
			api.openWin({
				name: 'submitOrder',
				url: 'submitOrder.html',
				vScrollBarEnabled: false,
				//				reload: true,
				pageParam: {
					oid: oid,
					status: 0
				}
			});
		},
		//登出
		layout: function() {
			var self = this;
			api.openWin({
				name: 'login',
				url: '../../html/login/login.html',
				vScrollBarEnabled: false,
				reload: true,
				pageParam: {
					openName: 'productMsg',
					cid: self.cid,
					type: 1
				}
			});
		}
	}
})